package com.example.demo.web;

import com.example.demo.mapper.firstsqlserver.GroupInfoMapper;
//import com.example.demo.mapper.firstsqlserver.UserInfoMapper;
import com.example.demo.mapper.firstsqlserver.UserInfoMapper;
import com.example.demo.mapper.pgsql.CdrMapper;
import com.example.demo.pojo.Cdr;
import com.example.demo.pojo.Hour;
import com.example.demo.pojo.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class HourController {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private CdrMapper cdrMapper;

    @Autowired
    private GroupInfoMapper groupInfoMapper;

    @ResponseBody
    @RequestMapping("/listHour")
    public List<Cdr> listHour(Model model, @RequestParam("starttime")String starttime, @RequestParam("endtime")String endtime, @RequestParam("groupId")int groupId, @RequestParam(value = "start", defaultValue = "1")int start, @RequestParam(value = "size", defaultValue = "6")int size) throws Exception{
        System.out.println("starttime=" + starttime + ",endtime=" + endtime + ",groupId=" + groupId + ",start=" + start);
        List<Hour> hours = new ArrayList<>();
        List<UserInfo> users = userInfoMapper.listUserInfo(groupId);
        StringBuilder sb = new StringBuilder();
        System.out.println("size01=" + users.size());
        for (int i = 0; i < users.size(); i++) {
            sb.append("'" + users.get(i).getLoginName() + "',");
        }
        String gh = sb.toString();
        gh = gh.substring(0, gh.length() - 1);
        System.out.println("gh=" + gh);
        PageHelper.startPage(start, size);
        List<Cdr> cdrs = cdrMapper.listCdr(starttime, endtime, gh);
        for (int i = 0; i < cdrs.size(); i++) {
            Cdr cdr = cdrs.get(i);
            //int id = i + 1;
            String cGh = cdrs.get(i).getGh();
            System.out.println("i=" + i + ",gh=" + cdrs.get(i).getGh());
            String cName = userInfoMapper.getUserInfo(cdrs.get(i).getGh()).getcName();
            int total = cdrs.get(i).getTimes();
            int h = total / 3600;
            int m = (total % 3600) / 60;
            String telHour = h + "小时" + m + "分钟";
            String gName = groupInfoMapper.getGroupInfo(userInfoMapper.getUserInfo(cdrs.get(i).getGh()).getGroupId()).getcName();
            //cdr.setId(id);
            cdr.setcGh(cGh);
            cdr.setcName(cName);
            cdr.setgName(gName);
            cdr.setTelHour(telHour);
        }
        PageInfo<Cdr> page = new PageInfo<>(cdrs);
        List<Cdr> pages = page.getList();
        for (int i = 0; i < pages.size(); i++) {
            pages.get(i).setTotal(page.getTotal());
        }
        return pages;
    }

}
